import pandas as pd
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

# 加载数据集
data = pd.read_csv('wine.data', header=None)
# 数据预处理
features = data.iloc[:, 1:]  # 提取特征列
labels = data.iloc[:, 0]  # 提取标签列
# 筛选特定类别
selected_classes = [1, 2]  # 设置要筛选的类别
filtered_data = data[data[0].isin(selected_classes)]
# 特征缩放（标准化）
scaled_features = (filtered_data.iloc[:, 1:] - features.mean()) / features.std()
# PCA降维
pca = PCA(n_components=2)  # 设置降维后的维数
pca_features = pca.fit_transform(scaled_features)
# 输出PCA降维结果
pca_df = pd.DataFrame(data=pca_features, columns=['PC1', 'PC2'])
print(pca_df.head())
# LDA降维
lda = LinearDiscriminantAnalysis(n_components=2)
lda_features = lda.fit_transform(scaled_features, filtered_data[0])
# 输出LDA降维结果
lda_df = pd.DataFrame(data=lda_features, columns=['LDA1', 'LDA2'])
print(lda_df.head())
